<?php

class Account extends Core {
	public $error;
	
	public function get($account = null, $field = null) {
		$rs = $this->Database->query('SELECT 
			MEMB_STAT.ConnectTM AS last_connection,
			MEMB_STAT.ServerName AS last_server,
			MEMB_STAT.IP AS last_ip,
			MEMB_INFO.memb_guid AS id,
			MEMB_INFO.memb_name AS name,
			MEMB_INFO.memb___id AS account,
			MEMB_INFO.memb__pwd AS password,
			MEMB_INFO.sno__numb AS key_code,
			MEMB_INFO.mail_addr AS email,
			MEMB_INFO.fpas_ques AS question,
			MEMB_INFO.fpas_answ AS answer,
			MEMB_INFO.bloc_code AS block
			FROM MEMB_INFO
			LEFT JOIN MEMB_STAT ON (MEMB_INFO.memb___id = MEMB_STAT.memb___id COLLATE DATABASE_DEFAULT)
			WHERE MEMB_INFO.memb___id = %s			
		', $account)->fetch();
		$data = array();
		if (!empty($rs)) {
			$data = $rs;
			$data['question'] = htmlentities($data['question']);
			$data['answer'] = htmlentities($data['answer']);
			$data['last_connection'] = date('d-m-Y H:i:s', strtotime($data['last_connection']));
			if (Config::read('columns.golds.enable')) {
				$data['golds'] = $this->get_golds($data['account']);
			}
			if (Config::read('columns.cash.enable')) {
				$data['cash'] = $this->get_cash($data['account']);	
			}
			$data['characters'] = $this->get_characters($account);
		}
		return $data;
	}
	
	public function get_options($name = null, $where = null) {
		$where = is_null($where) ? '' : ' AND Mithos.dbo.options.type = ' . (int)$where; 
		$rs = $this->Database->query('SELECT
			Mithos.dbo.options.name,
			Mithos.dbo.options.identificator,
			Mithos.dbo.memb_options.*
			FROM MEMB_INFO
			INNER JOIN Mithos.dbo.memb_options ON (MEMB_INFO.memb_guid = Mithos.dbo.memb_options.memb_guid)
			INNER JOIN Mithos.dbo.options ON (Mithos.dbo.memb_options.option_id = Mithos.dbo.options.id)
			WHERE MEMB_INFO.memb___id = %s
		' . $where, $name)->fetch_all();
		return $rs;
	}	
	
	public function get_golds($name = null) {
		$rs = $this->Database->query('SELECT ' . $this->Config->columns['golds']['column_amount'] . ' AS total FROM ' . $this->Config->columns['golds']['table'] . ' WHERE ' . $this->Config->columns['golds']['column_username'] . ' = %s', $name)->fetch();
		return $rs['total'];
	}
	
	public function get_cash($name = null) {
		$rs = $this->Database->query('SELECT ' . $this->Config->columns['cash']['column_amount'] . ' AS total FROM ' . $this->Config->columns['cash']['table'] . ' WHERE ' . $this->Config->columns['cash']['column_username'] . ' = %s', $name)->fetch();
		return $rs['total'];
	}	

	public function get_characters($account = null) {
		$rs = $this->Database->query('SELECT Name AS name 
			FROM Character 
			WHERE AccountID = %s
		', $account)->fetch_all();
		$data = array();
		foreach ($rs as $key => $value) {
			$data[$key] = $this->Character->get($value['name']);
		}
		return $data;
	}	
	
	public function is_connected($name = null) {
		$rs = $this->Database->query('SELECT ConnectStat FROM MEMB_STAT WHERE memb___id = %s', $name)->fetch();
		return $rs['ConnectStat'];
	}
}

?>